<?php

//    MySqlWrapper PHP class.

//    Copyright (C) 2009 Henrik Nordquist.
//
//    This code is property of the CodeArray PHP project.  Rights Reserved!
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

define("MYSQLWRAPPER_NOT_CONNECTED", 10);
define("MYSQLWRAPPER_INVALID_HOSTNAME", 11);
define("MYSQLWRAPPER_INVALID_USERNAME", 12);
define("MYSQLWRAPPER_INVALID_PASSWORD", 13);
define("MYSQLWRAPPER_CONNECT_FAILED", 14);
define("MYSQLWRAPPER_CONNECT_DB_FAILED", 15);
define("MYSQLWRAPPER_CONNECT_SUCCESS", 16);
define("MYSQLWRAPPER_INVALID_QUERY_TYPE", 21);
define("MYSQLWRAPPER_QUERY_FAILED", 22);

class MySqlWrapper
{
	private $link;
	
	private $status;
	
	private $last_query = array();
	
	private $debug;
	
	private $construct_args = array();
	
	/**
	 * Constructor. Establishes a connection to the specified MySQL database server.
	 *
	 * @param string $hostname
	 * @param string $username
	 * @param string $password
	 * @param string[optional] $db
	 * @param bool[optional] $debug
	 * @return int
	 */
	public function __construct($hostname, $username, $password, $db = null, $debug = true)
	{
		$this->construct_args = func_get_args();
		
		if(!is_string($hostname))
		{
			return MYSQLWRAPPER_INVALID_HOSTNAME;
		}
		if(!is_string($username))
		{
			return MYSQLWRAPPER_INVALID_USERNAME;
		}
		if(!is_string($password))
		{
			return MYSQLWRAPPER_INVALID_PASSWORD;
		}
		$this->debug = is_bool($debug) ? $debug : true;
		
		$this->link = @mysql_connect($hostname, $username, $password);
		
		if(!$this->link)
		{
			if($this->debug)
			{
				echo mysql_error($this->link);
			}
			return MYSQLWRAPPER_CONNECT_FAILED;
		}
		
		if($db != null && $this->link)
		{
			if(!$this->selectDb($db))
			{
				return MYSQLWRAPPER_CONNECT_DB_FAILED;
			}
		}
		return MYSQLWRAPPER_CONNECT_SUCCESS;
	}
	
	/**
	 * Destructor. Closes connection.
	 *
	 */
	public function __destruct()
	{
		$this->close();
	}
	
	/**
	 * Selects database, can only be used when a connection IS established.
	 *
	 * @param string $newdb
	 * @return bool
	 */
	public function selectDb($newdb)
	{
		if(!$this->link || !is_string($newdb))
		{
			return false;
		}
		
		return @mysql_select_db($newdb, $this->link);
	}
	
	/**
	 * Query the current database.
	 *
	 * @param string $sql
	 * @return resource
	 */
	public function query($sql)
	{
		$this->last_query['string'] = $sql;
		
		if(!$this->link)
		{
			return MYSQLWRAPPER_NOT_CONNECTED;
		}
		
		if(!is_string($sql))
		{
			return MYSQLWRAPPER_INVALID_QUERY_TYPE;
		}
		
		$this->last_query['resource'] = @mysql_query($sql, $this->link);
		
		if(!$this->last_query['resource'])
		{
			if($this->debug)
			{
				echo mysql_error($this->link);
			}
			return MYSQLWRAPPER_QUERY_FAILED;
		}
		return $this->last_query['resource'];
	}
	
	/**
	 * Get number of rows found in the specified query
	 *
	 * @param resource $q
	 * @return int
	 */
	public function numRows($q)
	{
		if(!is_resource($q))
		{
			return false;
		}
		
		return @mysql_num_rows($q);
	}
	
	/**
	 * Returns an array with data from the specified query resource.
	 *
	 * @param resource $q
	 * @param int[optional] $type
	 * @return array
	 */
	public function fetchArray($q, $type = MYSQL_BOTH)
	{
		if(!is_resource($q))
		{
			return false;
		}
		
		return @mysql_fetch_array($q, $type);
	}
	
	/**
	 * Returns an object with data from the specified query resource.
	 *
	 * @param resource $q
	 * @param string[optional] $classname
	 * @param array[optional] $params
	 * @return object
	 */
	public function fetchObject($q, $classname = null, $params = null)
	{
		if(!is_resource($q))
		{
			return false;
		}
		
		return @mysql_fetch_object($q, $classname, $params);
	}
	
	/**
	 * Escapes a string to prevent SQL injections.
	 *
	 * @param string $string
	 * @return bool
	 */
	public function escape($string)
	{
		if(version_compare(PHP_VERSION, "4.3.0") == "-1")
		{
			return @mysql_escape_string($string);
		}
		else
		{
			if(!$this->link)
			{
				return MYSQLWRAPPER_NOT_CONNECTED;
			}
			
			return @mysql_real_escape_string($string, $this->link);
		}
	}
	
	/**
	 * Are we connected?
	 *
	 * @return bool
	 */
	public function isConnected()
	{
		return mysql_ping($this->link);
	}
	
	/**
	 * Free the memory!
	 *
	 * @param resource $q
	 * @return bool
	 */
	public function freeResult($q)
	{
		if(!is_resource($q))
		{
			return false;
		}
		
		return @mysql_free_result($q);
	}
	
	/**
	 * Closes the connection.
	 *
	 * @return bool
	 */
	public function close()
	{
		if($this->link)
		{
			return @mysql_close($this->link);
		}
		return false;
	}
}

?>
